package com.ts.proj.service.impl;

import com.ts.proj.entity.ProjectEntity;
import com.ts.proj.repository.ProjectRepository;
import com.ts.proj.service.ProjectService;
import com.ts.util.EntityAttributeUtil;
import com.ts.util.PageData;

import com.ts.base.service.impl.CommonServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


//说明 字符串：，作为替换字符串 LOWERPROJECT                PROJECT

@Service
public class ProjectServiceImpl extends CommonServiceImpl<ProjectEntity>  implements ProjectService {

    @Autowired
    private ProjectRepository progectRepository;

    @Override
    public List<ProjectEntity> findAll(PageData pageData, Integer start, Integer length) {
        Specification<ProjectEntity> sp = this.getSpecification(pageData, ProjectEntity.class);
        Sort sort = new Sort(Sort.Direction.ASC,"id");
        Pageable pageable = new PageRequest(start, length, sort);
        return progectRepository.findAll(sp,pageable).getContent();
    }

    @Override
    public List<ProjectEntity> findAll(PageData pageData) {
        Specification sp = this.getSpecification(pageData, ProjectEntity.class);
        return progectRepository.findAll(sp);
    }

    @Override
    public long count(PageData pageData) {
        Specification<ProjectEntity> sp = this.getSpecification(pageData, ProjectEntity.class);
        return progectRepository.count(sp);
    }

    @Override
    public boolean deleteByIds(Iterable<ProjectEntity> iterable) {
        progectRepository.deleteInBatch(iterable);
        return false;
    }

    @Override
    public List<ProjectEntity> saveAll(List<ProjectEntity> entities) {
        return progectRepository.saveAll(entities);
    }

    @Override
    public List<ProjectEntity> findAllById(Integer[] ids) {
        List<ProjectEntity> list = progectRepository.findAllById(Arrays.asList(ids));
        if(list.size()==0){
            ProjectEntity entity = new ProjectEntity();
            list.add(entity);
        }
        return list;
    }

    @Override
    public boolean deleteAll() {
        progectRepository.deleteAll();
        return false;
    }

    @Override
    public Specification getSpecification(PageData pageData, Class entityClass) {
        return new Specification() {
            public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> preList = new ArrayList();
                List<String> fieldList = EntityAttributeUtil.entityAttribute(entityClass);

                if(null!=pageData.get("pro_name_like")&&!"".equals(pageData.getString("pro_name_like"))){
                    preList.add(criteriaBuilder.like(root.get("proName"), "%"+pageData.get("pro_name_like").toString()+"%"));
                }

                if(null!=pageData.get("pro_company_like")&&!"".equals(pageData.getString("pro_company_like"))){
                    preList.add(criteriaBuilder.like(root.get("proCampany"),"%"+pageData.get("pro_company_like").toString()+"%"));
                }

                if(null!=pageData.get("pro_state")&&!"".equals(pageData.getString("pro_state"))){
                    preList.add(criteriaBuilder.equal(root.get("proState"),pageData.get("pro_state").toString()));
                }

                if(null!=pageData.get("pro_category")&&!"".equals(pageData.getString("pro_category"))){
                    preList.add(criteriaBuilder.equal(root.get("proCategory"),pageData.get("pro_category").toString()));
                }

                preList.add(criteriaBuilder.equal(root.get("deleteFlag"),"N"));
                Predicate[] p = new Predicate[preList.size()];
                return criteriaBuilder.and((Predicate[])preList.toArray(p));
            }
        };
    }

    @Override
    public List<ProjectEntity> findByProName(String proName) {
        return progectRepository.findByProName(proName);
    }


}